<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\SystemModelRequest;
use App\Model\Admin\SystemModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ModelController extends Controller
{
    protected $fields = [
        'menu_id' => '',
        'name' => '',
        'table_name' => '',
        'description' => '',
    ];

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        return view('admin.model.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data = [];
        $Menu = new MenuController();
        $data['parent'] =  $Menu->getMenuList(['id', 'pid', 'title']);
        return view('admin.model.add')->with('data',$data);
    }


    /**
     * 添加
     *
     * @param SystemModelRequest $request
     * @return array
     * @throws \Exception
     */
    public function store(SystemModelRequest $request)
    {
        $menu = new SystemModel();
        foreach ($this->fields as $k => $v){
            $menu -> $k = $request->get($k, $v);
        }
        // 创建表
        $CreateTable = new CreateTableController();
        $res = $CreateTable->createTable($menu->table_name);
        if ($res['code'] == 0) {
            // 保存菜单
            DB::beginTransaction();
            try {
                $menu->save();
                DB::commit();
                return $this->res_msg(0, $res['msg']);
            } catch (\Exception $e) {
                DB::rollBack();
                return $this->res_msg(3, $e->getMessage());
            }
        } else {
            return $this->res_msg($res['code'], $res['msg']);
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = [];
        // 获取修改模型
        $Model = new SystemModel();
        $model = $Model->findOrFail($id);
        $data['info'] = ['id' => $id];
        foreach (array_keys($this -> fields) as $v ){
            $data['info'][$v] = old($v, $model->$v);
        }
        // 菜单列表
        $Menu = new MenuController();
        $data['parent'] =  $Menu->getMenuList(['id', 'pid', 'title']);

        return view('admin.model.edit')->with('data',$data);
    }


    /**
     * 更新
     *
     * @param SystemModelRequest $request
     * @param $id
     * @return array
     */
    public function update(SystemModelRequest $request, $id)
    {
        $SystemModel = new SystemModel();
        $system_model = $SystemModel->findOrfail($id);
        foreach ($this->fields as $k => $v){
            $system_model -> $k = $request->get($k, $v);
        }
        if ($system_model->save()){
            return $this->res_msg(0,'修改成功');
        } else{
            return $this->res_msg(1,'保存失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    /**
     * 获取模型数据
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getModel(Request $request)
    {
        $page = $request->page ? : 1;
        $limit = $request->limit ? : 10;

        $data['code'] = 0;
        $data['msg'] = '成功';
        $data['count'] = 19;
        $SystemMenu = new SystemModel();
        $data['data'] = $SystemMenu
            ->select('id', 'name', 'table_name', 'description', 'created_at', 'updated_at')
            ->orderBy('id','desc')
            ->skip( $limit * ($page - 1))
            ->take($limit)
            ->get();
        return response()->json($data);
    }
}
